package mj.scott.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mj.scott.common.Result;
import mj.scott.pojo.entity.ShoppingCart;
import mj.scott.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车管理")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车（支持同一菜品+口味累加数量）
     */
    @PostMapping
    @ApiOperation("添加商品到购物车")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart cart = shoppingCartService.add(shoppingCart);
        return Result.success(cart);
    }

    /**
     * 减少购物车商品数量（减到0则删除）
     */
    @PostMapping("/sub")
    @ApiOperation("减少购物车商品数量")
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart cart = shoppingCartService.sub(shoppingCart);
        return Result.success(cart);
    }

    /**
     * 查询指定桌号的购物车列表
     * @param tableId 桌号ID
     */
    @GetMapping("/table/{tableId}")
    @ApiOperation("查询桌号购物车列表")
    public Result<List<ShoppingCart>> getByTableId(@PathVariable Long tableId) {
        List<ShoppingCart> cartList = shoppingCartService.getByTableId(tableId);
        return Result.success(cartList);
    }

    /**
     * 清空指定桌号的购物车
     * @param tableId 桌号ID
     */
    @DeleteMapping("/clean/{tableId}")
    @ApiOperation("清空桌号购物车")
    public Result<String> clean(@PathVariable Long tableId) {
        shoppingCartService.cleanByTableId(tableId);
        return Result.success("购物车已清空");
    }
}
